package com.cw2.structural.composite;

import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合模式
 * 组织层级结构的数据
 */
@Data
public class Menu {
    private Integer id;
    private String name;
    private String split = "-";

    private List<Menu> childs = new ArrayList<>();

    public Menu(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    /**
     * 为层级提供添加方法
     *
     * @param menu
     */
    void addChildMenu(Menu menu) {
        childs.add(menu);
    }

    /**
     * 提供层级遍历方法
     */
    void printMenu() {
        System.out.println(split +  name);
        if (!CollectionUtils.isEmpty(childs)){
            for (Menu child : childs) {
                child.split += split;
                child.printMenu();
            }
        }
    }
}
